import { createRouter, createWebHistory } from 'vue-router'
import Index from '@/views/Index'
import store from '@/store/index'
import { NAVIGATION_BAR } from '@/store/methodConstant'

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
    meta: { title: '首页',path:'/' },
    children: [
      //默认页面
      {
        path: '',
        component: () => import('@/views/Home'),
        meta: { title: '首页',path:'/' } },
      {
        path: 'system/role',
        component: () => import('@/views/role/Index'),
        meta: { title: '角色管理',path:'/system/role' ,index : 1}},
      { path: 'system/dept',
        component: () => import('@/views/dept/Index'),
        meta: { title: '部门管理',path:'/system/dept' ,index : 2}},
      { path: 'system/dict',
        component: () => import('@/views/dict/Index'),
        meta: { title: '系统字典',path:'/system/dict' ,index : 3}},
      { path: 'system/dictbiz',
        component: () => import('@/views/dictBiz/Index'),
        meta: { title: '业务字典',path:'/system/dictbiz' ,index : 4}},
      { path: 'system/user',
        component: () => import('@/views/user/Index'),
        meta: { title: '用户管理',path:'/system/user' ,index : 5}},
      { path: 'system/param',
        component: () => import('@/views/param/Index'),
        meta: { title: '系统参数',path:'/system/param' ,index : 6}},
      { path: 'system/post',
        component: () => import('@/views/post/Index'),
        meta: { title: '岗位管理',path:'/system/post' ,index : 7}},
      { path: 'system/menu',
        component: () => import('@/views/menu/Index'),
        meta: { title: '菜单管理',path:'/system/menu' ,index : 8}},
      { path: 'log',
        component: () => import('@/views/log/Index'),
        meta: { title: '通用日志',path:'/log' ,index : 9}},
      { path: 'itconfig',
        component: () => import('@/views/IfConfiguration/Index'),
        meta: { title: '接口配置',path:'/itconfig' ,index : 10}},
      { path: 'interfaceQuota',
        component: () => import('@/views/ifQuota/Index'),
        meta: { title: '接口配额',path:'/interfaceQuota' ,index : 11}},
      { path: 'use/param',
        component: () => import('@/views/useapiparam/Index'),
        meta: { title: '请求常量',path:'/use/param' ,index : 12}},
      { path: 'use/supplier',
        component: () => import('@/views/useapisupplier/Index'),
        meta: { title: '供应商',path:'/use/supplier' ,index : 13}},
      { path: 'use/tripartite',
        component: () => import('@/views/useapi/Index'),
        meta: { title: 'API配置',path:'/use/tripartite' ,index : 14}},
      { path: 'comment',
        component: () => import('@/views/messageCenter/Index'),
        meta: { title: '留言中心',path:'/comment' ,index : 15}},
      { path: 'online',
        component: () => import('@/views/online/Index'),
        meta: { title: '在线列表',path:'/online' ,index : 16}},
  ]
  },
  /* 登录页 */
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login')
  },
  /* 路由匹配不到就显示404 */
  {
    path: '/:catchAll(.*)',
    name: '/404',
    component: () => import('@/views/404/404')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//前缀导航
router.beforeEach((to, from) => {
  //前往登录页面时，直接通行
  if (to.path === '/login'){
    return true
  }
  //设置页面标题
  if (to.meta !== undefined && to.meta.title !== undefined){
    window.document.title = 'TAO - ' + to.meta.title
  }else {
    window.document.title = 'TAO'
  }
  //查看是否前往首页，当无token信息时前往首页需要跳转到登录页面
  if ((to.path === '/' || to.path === '') && store.getters.token === null){
    router.push('/login')
  }
  //获得导航条数据
  let navigationBar = store.getters.navigationBar
  //寻找是否已经加入过导航条
  let find = navigationBar ===null ? null : navigationBar.find((value) => value.path === to.meta.path)
  //未加入则新增到导航条中
  if ((find === null || find === undefined)  && to.meta.path !== undefined){
      navigationBar.push({title:to.meta.title,path:to.meta.path})
      store.commit(NAVIGATION_BAR,navigationBar)
  }
  return true
})


export default router
